﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using GameEngine.Graphics;

namespace GameEngine.TileMap
{
    public class TileMap
    {
        public Int32[,] TextureMap;
        public Texture2D[] TextureMapTexture;

        private Int32[,] LayerMap;
        
        private Int32[,] EnterableMap;

        public Int32[,] LightMap;
        public Texture2D[] LightMapTexture;

        public Int32 TilesX;
        public Int32 TilesY;

        public void Init()
        {

        }

        public void LoadContent(ContentManager Content)
        {
            #region Lese Infos ein

            Int32 TexturesCount, LightMapTexturesCount;

            StreamReader SR = new StreamReader(@"Content\TileMap\Map.txt");
            TilesX = Convert.ToInt32(SR.ReadLine());
            TilesY = Convert.ToInt32(SR.ReadLine());
            TexturesCount = Convert.ToInt32(SR.ReadLine());
            LightMapTexturesCount = Convert.ToInt32(SR.ReadLine());
            TextureMap = new Int32[TilesX, TilesY];
            LayerMap = new Int32[TilesX, TilesY];
            EnterableMap = new Int32[TilesX, TilesY];
            LightMap = new Int32[TilesX, TilesY];

            TextureMapTexture = new Texture2D[TexturesCount];
            LightMapTexture = new Texture2D[LightMapTexturesCount];

            for (int i = 0; i < TextureMapTexture.Length; i++)
            {
                TextureMapTexture[i] = Content.Load<Texture2D>(SR.ReadLine());
            }
            for (int i = 0; i < LightMapTexture.Length; i++)
            {
                LightMapTexture[i] = Content.Load<Texture2D>(SR.ReadLine());
            }
            SR.Close();

            #endregion

            #region Lade Maps

            SR = new StreamReader(@"Content\TileMap\TextureMap.map");
            for (int y = 0; y < TilesX; y++)
            {

                    string Line = ""; Line = SR.ReadLine();
                    string[] Chars = Line.Split(',');
                    int i = -1;
                    foreach (string Info in Chars)
                    {
                        i++;
                        TextureMap[y,i] = Convert.ToInt32(Info);
                    }

            }
            SR.Close();

            SR = new StreamReader(@"Content\TileMap\EnterableMap.map");
            for (int y = 0; y < TilesX; y++)
            {

                string Line = ""; Line = SR.ReadLine();
                string[] Chars = Line.Split(',');
                int i = -1;
                foreach (string Info in Chars)
                {
                    i++;
                    EnterableMap[y, i] = Convert.ToInt32(Info);
                }

            }
            SR.Close();

            SR = new StreamReader(@"Content\TileMap\LightMap.map");
            for (int y = 0; y < TilesX; y++)
            {

                string Line = ""; Line = SR.ReadLine();
                string[] Chars = Line.Split(',');
                int i = -1;
                foreach (string Info in Chars)
                {
                    i++;
                    LightMap[y, i] = Convert.ToInt32(Info);
                }

            }
            SR.Close();

            SR = new StreamReader(@"Content\TileMap\LayerMap.map");
            for (int y = 0; y < TilesX; y++)
            {

                string Line = ""; Line = SR.ReadLine();
                string[] Chars = Line.Split(',');
                int i = -1;
                foreach (string Info in Chars)
                {
                    i++;
                    LayerMap[y, i] = Convert.ToInt32(Info);
                }

            }
            SR.Close();

            #endregion
        }

        public void Update()
        {

        }

        public void Draw()
        {
            Camera.SpriteBatch.Begin();
            for (int y = 0; y < TilesY; y++)
            {
                for (int x = 0; x < TilesX; x++)
                {
                    Vector2 Temp = new Vector2(y * 100, x * 100);
                    //Camera.TryDraw(TextureMapTexture[TextureMap[x, y]], Temp, null, Color.White, 0f, Temp, 0f, SpriteEffects.None, 0f);
                    Camera.TryDraw(TextureMapTexture[TextureMap[x, y]], Temp, Color.White);
                }
            }
            Camera.SpriteBatch.End();
        }
    }
}
